<!-- 工厂配置： 蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex AIC">
          <h1 class="f16">所属品牌</h1>
          <AddOne :tableHeader="tableHeader2" :tableData="tableData2" @AddHandle="AddHandle" :unAdd="false" :unSave="true" />
        </div>
        <JTable
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          class="mt10"
          :isLoading="isLoading2"
          @currentRow="currentRow2"
          @download="download"
          @visibleChangeV2="visibleChangeV2"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex AIC">
          <h1 class="f16">所属工厂</h1>
          <AddOne :tableHeader="tableHeader3" :tableData="tableData3" :unAdd="false" :unSave="true" />
        </div>
        <JTable
          :tableHeader="tableHeader3"
          :tableData="tableData3"
          :isLoading="isLoading2"
          @currentRow="currentRow2"
          @download="download2"
          @visibleChangeV2="visibleChangeV3"
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { selectList } from '@/plugins/mixin'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { defineComponent, onMounted, ref, watch } from 'vue'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import qrcode from 'qrcode'
import '@/assets/font/SmileySans-Oblique-normal.js'
import { Plus } from '@element-plus/icons-vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PlantConfiguration'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const showSecondPage = ref(false)
const uploadListDialog = ref(false)
const fileList = ref<any[]>([])
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const drawer = ref(false)
const handlePictureCardPreview: any['onPreview'] = (uploadFile: any) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}

const confirmUpload = async () => {
  let formData = new FormData()
  console.log(fileList.value)
  fileList.value.forEach((item: any) => {
    formData.append('list', item.raw) // 添加文件对象及文件名
  })
  const res = (await fileRequest.post('/pcOrder/importMorePhotos', formData)) as any
  res.data
    .text()
    .then(function (text: any) {
      // 将文本转换为JSON对象
      var jsonData = JSON.parse(text)
      if (jsonData.code == 200) {
        JMessage.success('上传成功')
        fileList.value = []
        uploadListDialog.value = false
        getTableList()
      } else {
        JMessage.error(jsonData.message)
      }
    })
    .catch(function (error: any) {
      console.error('Error reading Blob as text:', error)
    })
}
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入用户名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
const tableHeader = ref([
  {
    cfmFlag: '#c0e080',
    label: '用户ID',
    prop: 'userId'
  },
  {
    label: 'QC登录名',
    prop: 'loginName'
  },
  {
    label: '用户名称',
    prop: 'userName'
  }
])
// 表一模块
const printArr = ref() as any
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const isLoading = ref(true)

// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)

const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const order = ref('')
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/subcontract/config/user/list', {
    userName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入用户名称'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 排序
const sortChange = (sort: string) => {
  if (sort === 'ascending') {
    console.log('升序')
    order.value = '1'
  } else if (sort === 'descending') {
    console.log('降序')
    order.value = '2'
  } else {
    console.log('取消排序')
    order.value = ''
  }
  getTableList()
}
// 修改后点击确定按钮
const confirmChange = async (row: any) => {
  await service.put('/pcOrder/updatePcOrderData', { list: [row] })
}
// 勾选
const selectedLists = ref([]) as any
const selectedFacList = ref([]) as any
const selectedList = (selected: any) => {
  selectedLists.value = selected
  printArr.value = selectedLists.value.map((item: any) => {
    let qrCodeData = item.lotNo
    qrcode.toDataURL(qrCodeData, (err: any, url: any) => {
      if (err) {
        JMessage.error(err)
      } else {
        qrCodeData = url
      }
    })
    return {
      img: qrCodeData,
      lotNo: item.lotNo,
      facStyle: item.facStyle,
      color: item.color
    }
  })
  selectedFacList.value = selected.map((item: any) => {
    return item.lotNo
  })
}

// 点击某一行
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
  await getTableList2()
  await getTableList3()
}

const tableHeader2 = ref([
  {
    label: '品牌编号',
    prop: 'brandCode'
  },
  {
    width: '200px',
    type: 'selectV2',
    label: '品牌简称',
    prop: 'brandName',
    options: []
  },
  {
    type: 'function',
    label: '操作',
    prop: [
      {
        label: '删除',
        bgColor: '#1890FF'
      }
    ]
  }
])
const tableData2 = ref([])
// 表一模块
const isLoading2 = ref(true)
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>(`/subcontract/config/brand/ome?userId=${currentTableRow.value.userId}`)
  isLoading2.value = false
  tableData2.value = tableDataList
  currentRow2()
}, 500)
const currentTableRow2 = ref({}) as any
const currentRow2 = (row: any = tableData2.value[0]) => {
  currentTableRow2.value = row
}

const tableHeader3 = ref([
  {
    label: '厂商编号',
    prop: 'vendorNo'
  },
  {
    width: '200px',
    type: 'selectV2',
    label: '厂商名称',
    prop: 'vendorAbbr',
    options: []
  },
  {
    type: 'function',
    label: '操作',
    prop: [
      {
        label: '删除',
        bgColor: '#1890FF'
      }
    ]
  }
])
const tableData3 = ref([])
const isLoading3 = ref(true)
const currentTableRow3 = ref({}) as any
const currentRow3 = (row: any = tableData3.value[0]) => {
  currentTableRow3.value = row
}
const total3 = ref(0)
const getTableList3 = async () => {
  isLoading3.value = true
  let tableDataList = await service.post<any, any>(`/subcontract/config/user/ome?userId=${currentTableRow.value.userId}`)
  tableData3.value = tableDataList
  isLoading3.value = false
  currentRow3()
}

// 删除
const download = async (row: any) => {
  const res = (await LoginWork.post(`/subcontract/config/brand/ome/delete`, row)) as any
  if (res.code === 200) {
    JMessage.success('删除成功')
    getTableList2()
  } else {
    JMessage.error(res.message)
  }
}
const download2 = async (row: any) => {
  const res = (await LoginWork.post(`/subcontract/config/user/ome/delete`, row)) as any
  if (res.code === 200) {
    JMessage.success('删除成功')
    getTableList3()
  } else {
    JMessage.error(res.message)
  }
}

const AddHandle = async (row: any, arr: any) => {
  const res = (await LoginWork.post(`/subcontract/config/brand/ome`)) as any
  row.options = res.data.map((item: any) => {
    item.label = `${item.brandCode}  ${item.brandName}`
    item.value = `${item.brandCode}  ${item.brandName}`
    return item
  })
}
const visibleChangeV2 = async (visible: boolean, row: any, prop: string) => {
  if (!visible && row.brandName) {
    const res = (await LoginWork.post(`/subcontract/config/brand/ome/add`, {
      ...currentTableRow.value,
      brandCode: row.brandName.split('  ')[0],
      brandName: row.brandName.split('  ')[1]
    })) as any
    if (res.code === 200) {
      JMessage.success('新增成功')
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
}
const visibleChangeV3 = async (visible: boolean, row: any, prop: string) => {
  if (!visible && row.vendorAbbr) {
    const res = (await LoginWork.post(`/subcontract/config/user/ome/add`, {
      ...currentTableRow.value,
      ...currentTableRow3.value,
      vendorNo: row.vendorAbbr.split('  ')[0],
      vendorAbbr: row.vendorAbbr.split('  ')[1]
    })) as any
    if (res.code === 200) {
      JMessage.success('新增成功')
      getTableList3()
    } else {
      JMessage.error(res.message)
    }
  }
}
onMounted(async () => {
  const res = (await LoginWork.post(`/subcontract/config/combo`)) as any
  tableHeader2.value[1].options = res.data.map((item: any) => {
    item.label = `${item.brandCode}  ${item.brandShortName}`
    item.value = `${item.brandCode}  ${item.brandShortName}`
    return item
  })

  const res2 = (await LoginWork.post(`/subcontract/static/oem/combo`)) as any
  tableHeader3.value[1].options = res2.data.map((item: any) => {
    item.label = `${item.vendorNo}  ${item.vendorAbbr}`
    item.value = `${item.vendorNo}  ${item.vendorAbbr}`
    return item
  })
})
</script>
<style media="print">
.List {
  font-size: 13px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}

.left {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
}

.right {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: start;
}

img {
  width: 80px;
  height: 80px;
}
</style>
<style type="text/css" media="print">
@media print {
  html,
  body {
    margin: 0;
    padding: 0;
    width: 60mm;
    height: 40mm;
  }

  @page {
    margin: 1mm;
    margin-top: 4mm;
    size: landscape;
  }
}
</style>
<style lang="less" scoped>
p {
  color: #ff2020;
  font-size: 12px;
}
</style>
